package ar.com.larctest.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import ar.com.larctest.entities.Horses;
import ar.com.larctest.R;

public class ListViewMasterDataHorsesAdapter extends ArrayAdapter<Horses>
{
	public List<Horses> items;
	Activity activity;

	public ListViewMasterDataHorsesAdapter(Activity activity, int textViewResourceId, List<Horses> items) {
		super(activity, textViewResourceId, items);
		this.items = items;
		this.activity = activity;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Horses getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return (items.get(position)).getIdHorse();
	}

	private class ViewHolder {
		ImageView imgSelected;
	    TextView name;
	    TextView hair;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		LayoutInflater inflater =  activity.getLayoutInflater();
		
		if (convertView == null)
		{
            if(activity.getClass().getSimpleName().equals("PopUpListHorsesActivity"))
                convertView = inflater.inflate(R.layout.listitem_horses_popup, null);
            else
			    convertView = inflater.inflate(R.layout.listitem_horses, null);
			holder = new ViewHolder();
			holder.imgSelected = (ImageView) (convertView != null ? convertView.findViewById(R.id.imgSelected) : null);
			holder.name = (TextView) (convertView != null ? convertView.findViewById(R.id.name) : null);
			holder.hair = (TextView) (convertView != null ? convertView.findViewById(R.id.hair) : null);

            if (convertView != null) {
                convertView.setTag(holder);
            }
        }
		else
			holder = (ViewHolder) convertView.getTag();

        Horses horseItem = items.get(position);
		if(horseItem.getSelected()){
            if(activity.getClass().getSimpleName().equals("PopUpListHorsesActivity"))
                holder.imgSelected.setImageResource(R.drawable.ic_sel_item_white);
            else
                holder.imgSelected.setImageResource(R.drawable.ic_sel_item);
        }
		else {
            if(activity.getClass().getSimpleName().equals("PopUpListHorsesActivity"))
                holder.imgSelected.setImageResource(R.drawable.ic_unsel_item_white);
            else
                holder.imgSelected.setImageResource(R.drawable.ic_unsel_item);
        }
		holder.name.setText(horseItem.getName());
		holder.hair.setText(horseItem.getHair());

		return convertView;	
	}
}
